package com.winhxd.b2c.product.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.winhxd.b2c.common.context.AdminUser;
import com.winhxd.b2c.common.context.CustomerUser;
import com.winhxd.b2c.common.context.UserContext;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.product.condition.ProductTagCondition;
import com.winhxd.b2c.common.domain.product.model.ProductTag;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.product.dao.ProductTagMapper;
import com.winhxd.b2c.product.service.ProductTagService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author cuipengbo
 * @create 2019-01-05 17:19
 * @desc 商品服务管理
 **/
@Service
public class ProductTagServiceImpl implements ProductTagService {

    private Logger logger = LoggerFactory.getLogger(ProductTagServiceImpl.class);

    private static final int TAG_STATUS = 0;
    private static final int PRODUCT_TAG_STATUS = 1;
    private static final int DEFAUTE_STATUS = 0;

    @Autowired
    private ProductTagMapper productTagMapper;

    @Override
    public Integer modifyProductTagStatusById(ProductTagCondition condition) {
        logger.info("modifyProductTagStatusById方法入参是：" + JsonUtil.toJSONString(condition));
        ProductTag productTag = new ProductTag();
        productTag.setModifiedTime(new Date());
        if(condition.getStatus() != null){
            if(condition.getStatus() == TAG_STATUS){
                productTag.setStatus(PRODUCT_TAG_STATUS);
            }else {
                productTag.setStatus(TAG_STATUS);
            }
        }
        productTag.setId(condition.getId());
        return productTagMapper.updateByPrimaryKeySelective(productTag);
    }

    @Override
    public Integer deleteProductTagById(Long id) {
        return productTagMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PagedList<ProductTag> findProductTagList(ProductTagCondition condition) {
        logger.info("findProductTagList方法入参是：" + JsonUtil.toJSONString(condition));
        Page page = PageHelper.startPage(condition.getPageNo(), condition.getPageSize());
        List<ProductTag> productTags = productTagMapper.selectProductTagList();
        PagedList<ProductTag> pagedList = new PagedList<>();
        pagedList.setData(productTags);
        pagedList.setPageNo(condition.getPageNo());
        pagedList.setPageSize(condition.getPageSize());
        pagedList.setTotalRows(page.getTotal());
        return pagedList;
    }

    @Override
    public ProductTag getProductTagById(Long id) {
        return productTagMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer savaProductTag(ProductTagCondition condition) {
        logger.info("savaProductTag方法入参是：" + JsonUtil.toJSONString(condition));
        ProductTag productTag = new ProductTag();
        BeanUtils.copyProperties(condition, productTag);
        productTag.setCreatedTime(new Date());
        productTag.setStatus(PRODUCT_TAG_STATUS);
        productTag.setDefaultStatus(DEFAUTE_STATUS);
        //添加排序
        int sort = productTagMapper.selectMaxSort();
        productTag.setSort(sort + 1);
        return productTagMapper.insertSelective(productTag);
    }

    @Override
    public Integer savaOrUpdateProductTag(ProductTagCondition condition) {
        logger.info("savaOrUpdateProductTag方法入参是：" + JsonUtil.toJSONString(condition));
        ProductTag productTag = new ProductTag();
        BeanUtils.copyProperties(condition, productTag);
        //服务id不是空则修改
        Integer i = 1;
        try {
            if(productTag.getId() != null){
                productTag.setModifiedTime(new Date());
                productTag.setModifiedBy(condition.getCreatedBy());
                productTagMapper.updateByPrimaryKeySelective(productTag);
            }else {
                productTag.setCreatedBy(condition.getCreatedBy());
                productTag.setCreatedTime(new Date());
                productTag.setStatus(PRODUCT_TAG_STATUS);
                productTag.setDefaultStatus(DEFAUTE_STATUS);
                //添加排序
                Integer sort = productTagMapper.selectMaxSort();
                if(sort != null){
                    productTag.setSort(sort + 1);
                }else {
                    // 还没有排序时
                    productTag.setSort(1);
                }
                productTagMapper.insertSelective(productTag);
            }
        }catch (Exception e){
            i = 0;
            logger.error("保存商品服务失败", e);
        }
        return i;
    }

    @Override
    public List<ProductTag> findProductTagByModel(ProductTagCondition condition) {
        ProductTag productTag = new ProductTag();
        BeanUtils.copyProperties(condition, productTag);
        return productTagMapper.selectProductTagByModel(productTag);
    }

    @Override
    public Integer modifyProductTagById(ProductTagCondition condition) {
        ProductTag tag = new ProductTag();
        BeanUtils.copyProperties(condition,tag);
        return productTagMapper.updateByPrimaryKeySelective(tag);
    }
}
